package New;

import lombok.Getter;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**电商比价需求
 * 同一款商品，同时搜索出同款产品在各大电商的售价
 * 同一款产品，同时搜索出本产品在某一个电商平台下，各个入住门店的售价是多少
 * 出来结果希望是同款产品在不同地方的价格清单列笔啊，返回一个list<String>
 * 《mysql》in jd price is  88.05
 *  《mysql》 in taobao price is 86.11
 *  要求深刻理解
 *      函数式编程
 *      链式编程
 *      Stream流式计算
 */
class NetMall{
    @Getter
    private String mallName;
    public NetMall(String mallName){
        this.mallName = mallName;
    }
    public double calcPrice(String productName){
//        检索需要1秒钟
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       return ThreadLocalRandom.current().nextDouble()*2+productName.charAt(0);
    }
}
public class CompletableFutureNetMallDemo {
        static List<NetMall> list = Arrays.asList(
        new NetMall("jd"),
        new NetMall("pdd"),
        new NetMall("tmall"),
        new NetMall("dangdangwang"),
        new NetMall("taobao"));
//        同步 step by step
        public static  List<String> getPriceByStep(List<NetMall> list,String productName){
            return list.stream().map(netMall -> String.format(
                    productName + " in %s price is %.2f", netMall.getMallName(),
                    netMall.calcPrice(productName))).collect(Collectors.toList());
        }
        //异步 万箭齐发
        public static List<String> getPriceByAsync(List<NetMall> list,String productName){
            return list.stream().map(netMall -> CompletableFuture.supplyAsync(
                    () -> String.format(productName + " in %s price is %.2f",
                    netMall.getMallName(), netMall.calcPrice(productName)))).collect(Collectors.toList())
                    .stream().map(CompletableFuture::join).collect(Collectors.toList());
        }
    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        List<String> list1 = getPriceByStep(list, "mysql");
        for (String s : list1) {
            System.out.println(s);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("----costTime:"+(endTime-startTime)+"毫秒");
        System.out.println("***************************");

        long startTime2 = System.currentTimeMillis();
        List<String> list2 = getPriceByAsync(list,"mysql");
        for (String s : list2) {
            System.out.println(s);
        }
        long endTime2 = System.currentTimeMillis();
        System.out.println("----costTime:"+(endTime2-startTime2)+"毫秒");
    }
}
